package com.example.flex.controller;

import com.example.flex.domain.RoleMenuEntity;
import com.example.flex.utils.response.BaseResponse;
import com.example.flex.service.RoleMenuService;
import com.example.flex.utils.common.PageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import java.util.List;
import java.util.Arrays;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 角色菜单关联表控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "角色菜单关联表接口")
@RequestMapping("roleMenu")
public class RoleMenuController{

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 保存
     *
     * @param roleMenuEntity 角色菜单关联表
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存角色菜单关联表")
    public BaseResponse<?> save(@RequestBody @ApiParam("角色菜单关联表") RoleMenuEntity roleMenuEntity){
        boolean saveFlag = roleMenuService.save(roleMenuEntity);
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询角色菜单关联表")
    public BaseResponse<?> info(@PathVariable @ApiParam("角色菜单关联表主键") Integer id){
        return BaseResponse.success(roleMenuService.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询角色菜单关联表")
    public BaseResponse<?> list(@RequestBody @ApiParam("角色菜单关联表分页参数") PageEntity<RoleMenuEntity> roleMenuEntityPage){
        RoleMenuEntity roleMenuEntity = roleMenuEntityPage.getData();
        QueryWrapper queryWrapper = getQueryWrapper(roleMenuEntity);
        return BaseResponse.success(roleMenuService.page(new Page<>(roleMenuEntityPage.getPageNum(),roleMenuEntityPage.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除角色菜单关联表")
    public BaseResponse<?> delete(@PathVariable @ApiParam("角色菜单关联表主键(列表)") Integer[] ids){
        return BaseResponse.success(roleMenuService.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param roleMenuEntity 角色菜单关联表
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改角色菜单关联表")
    public BaseResponse<?> edit(@RequestBody @ApiParam("角色菜单关联表") RoleMenuEntity roleMenuEntity){
        return BaseResponse.success(roleMenuService.updateById(roleMenuEntity),"修改成功");
    }

    /**
     * 获取queryWrapper
     *
     * @param roleMenuEntity 角色菜单关联表
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(RoleMenuEntity roleMenuEntity){
        return QueryWrapper.create()
                    .eq(RoleMenuEntity::getRoleId,roleMenuEntity.getRoleId(),roleMenuEntity.getRoleId()!=null)
                    .eq(RoleMenuEntity::getMenuId,roleMenuEntity.getMenuId(),roleMenuEntity.getMenuId()!=null);
    }
}